//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef _SEARCH_PANEL_YG_H__
#define _SEARCH_PANEL_YG_H__

#include "def.h"

class CSearchPanel: public wxPanel
{
public:
								CSearchPanel(wxWindow* parent, ISearchProvider*	pSearch);
	virtual						~CSearchPanel(void);

	bool						Match(const SEARCHFIELD& sf, const wxString& strVal) const;
	void						Save();
	void						Load();


protected:
	void						OnSearch(wxCommandEvent& ev);
	void						OnSearchNext(wxCommandEvent& ev);
	void						OnSearchChanged(wxCommandEvent& ev);
	void						OnClear(wxCommandEvent& ev);

	void						OnFilterCfg(wxCommandEvent& ev);
	void						OnFilterChanged(wxCommandEvent& ev);

	DECLARE_EVENT_TABLE()


private:
	wxSizer*					CreateSearch();
	wxSizer*					CreateFilter();


	void						InitSearchFields();
	int							GetCurrentSearchField();
	void						AddValueToVocablry(const wxString& strSearch);
	void						ClearVocablry();
	int							DoSearch(const wxString& val, unsigned int iFieldID, size_t posStart);

	bool						GetRuleSet(const wxString& strRuleName, FIELDRULESET& rlSet) const;
	bool						ApplyRule(const FILTERFIELDRULE& rule, const SEARCHFIELD& sf, const wxString& strVal) const;
	void						SetDefaultFilter();
private:
	//Search
	class wxComboBox*			m_pcSearch;
	class wxComboBox*			m_pcSearchFields;
	class wxButton*				m_pSearchButton;
	class wxButton*				m_pSearchNextButton;
	class wxButton*				m_pSearchClearButton;

	//Filter
	class wxButton*				m_pFilterButton;
	class wxComboBox*			m_pcFilterSelector;

	ISearchProvider*			m_pSearch;
	int							m_nCurSearchPos;
	FILTERS						m_filters;
};

class CFilterManager: public wxDialog
{
public:
								CFilterManager(wxWindow* parent, ISearchProvider* pSearch, FILTERS&	filters);

private:
	void						AddRuleTable(class wxGridBagSizer *pGrid, int& nRowCount);
	void						RemoveFilter(const wxString& strFilterName);

	bool						GetFilter(const wxString& strName, FIELDRULESET& rSet);
	bool						GetRule(unsigned int iFieldID, const FIELDRULESET& rSet, FILTERFIELDRULE& rule);
	bool						GetCurRule(unsigned int iFieldID, FILTERFIELDRULE& rule);
	void						SetCurRule( FILTERFIELDRULE& rule );

	void						ShowFilter(const wxString& strName);
	void						ShowFilterFields(FIELDRULESET& rSet);
	void						EnableFilterFieids(bool bEnable=true);
	void						RefreshFilter();
	void						SelectItem( class wxComboBox* pBox, cFilterRule	rule);
	void						LoadFilters();

private:
	void						OnAdd(wxCommandEvent& ev);
	void						OnRemove(wxCommandEvent& ev);
	void						OnFilterSelected(wxCommandEvent& ev);
	void						OnRuleUpdated(wxCommandEvent& ev);
	void						OnRuleValUpdated(wxCommandEvent& ev);
	DECLARE_EVENT_TABLE()

private:
	class wxComboBox**			m_ppRuleCombos;
	class wxTextCtrl**			m_ppRuleValues;
	ISearchProvider*			m_pSearch;
	FILTERS&					m_filters;
	class wxButton*				m_pRemoveButton;
	class wxButton*				m_pAddButton;
	class wxComboBox*			m_pcFilterSelCombo;

	SEARCHFIELD*				m_sf;
	size_t						m_sz;
};

#endif //_SEARCH_PANEL_YG_H__
